package com.example.system.controller;

import com.example.common.util.Result;
import com.example.dto.system.SysMenuRoleDto;
import com.example.system.SysMenu;
import com.example.system.service.impl.SysMenuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuServiceImpl sysMenuService;

    @ApiOperation("获取菜单列表")
    @GetMapping("findNodes")
    public Result findNodes(){
        return sysMenuService.findNodes();
    }

//  GET  http://localhost:9528/dev-api/system/admin/system/sysMenu/remove/43
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        return sysMenuService.remove(id);
    }

//  POST  http://localhost:9528/dev-api/system/admin/system/sysMenu/save
    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu){
        if(sysMenu == null){
            return Result.fail();
        }
        boolean save = sysMenuService.save(sysMenu);
        return Result.ok(save);
    }

//    PUT http://localhost:9528/dev-api/system/admin/system/sysMenu/update
    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody SysMenu sysMenu){
        if(sysMenu == null){
            return Result.fail();
        }
        boolean b = sysMenuService.updateById(sysMenu);
        return Result.ok(b);
    }

//   GET http://localhost:9528/dev-api/system/admin/system/sysMenu/toAssign/1
    @GetMapping("toAssign/{roleId}")
    @ApiOperation(value = "查看某个角色的权限列表")
    public Result toAssign(@PathVariable("roleId") Long roleId){
        return sysMenuService.toAssign(roleId);
    }

//   POST http://localhost:9528/dev-api/system/admin/system/sysMenu/doAssign
    @ApiOperation(value = "给某个角色授权")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody SysMenuRoleDto sysMenuRoleDto){
        return sysMenuService.doAssign(sysMenuRoleDto);
    }


}
